package com.bringway.p2p.controller;

import java.io.OutputStream;
import java.net.URLDecoder;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.framework.controller.JsonController;
import com.bringway.framework.excel.ExcelOperUtil;
import com.bringway.framework.excel.ExcelSheet;
import com.bringway.framework.util.DateUtils;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.p2p.service.P2PGrtzzqService;
import com.bringway.p2p.service.P2PJhntzxiService;

import net.sf.json.JSONObject;

/*
 * @author fan
 * @className P2PGrtzzqController
 * @description 个人投资债权信息控制器
 * @date 2016年1月4日上午11:19:19
 */
@Controller
@RequestMapping("/jhntzxi")
public class P2PJhntzxiController extends JsonController{
	@Autowired
	private P2PJhntzxiService p2pjhntzxiService;
	/**
	 * @方法名称:grtzzqQuery
	 * @方法描述:查询个人投资债权信息
	 * @author fan
	 * @date 2016年1月4日上午11:20:26
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryJhntzxi")
	public ModelAndView queryJhntzxi(HttpServletRequest request ,HttpServletResponse response)throws Exception{
		String TZSJ_START = RequestUtils.getStringParameter(request, "startDate");
		String TZSJ_END = RequestUtils.getStringParameter(request, "endDate");
		String userId = RequestUtils.getStringParameter(request, "USERID");
		String REF_XM_ORDER = RequestUtils.getStringParameter(request, "REF_XM_ORDER");
		String HTBH = RequestUtils.getStringParameter(request, "HTBH");
		String TZZT = RequestUtils.getStringParameter(request, "TZZT");
		String HKZT = RequestUtils.getStringParameter(request, "HKZT");
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		JSONObject parms = new JSONObject();

		parms.put("userId", userId);
		parms.put("TZSJ_START", TZSJ_START);
		parms.put("TZSJ_END", TZSJ_END);
		parms.put("HKZT", HKZT);
		parms.put("REF_XM_ORDER", REF_XM_ORDER);
		parms.put("TZZT", TZZT);
		parms.put("HTBH", HTBH);
		parms.put("start", start);
		parms.put("limit", limit);
		
		
		List list = p2pjhntzxiService.queryJhntzxi(parms);
		int total = p2pjhntzxiService.getPageCount(parms);
		String Sum = p2pjhntzxiService.getJhntzJe(parms);
		JSONObject result = new JSONObject();
		result.put("data", list);
		result.put("total", total);
		result.put("Sum", Sum);
		serialize(request, response, result);
		return null;
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @return  导出excel表格
	 * @throws Exception
	 */
	@RequestMapping("exportJhntzxiExcelMethod")
	public ModelAndView exportTzrxxExcelMethod(HttpServletRequest request, HttpServletResponse response) throws Exception{

		JSONObject parms = new JSONObject();
		String TZSJ_START = RequestUtils.getStringParameter(request, "startDate");
		String TZSJ_END = RequestUtils.getStringParameter(request, "endDate");
		String userId = RequestUtils.getStringParameter(request, "USERID");
		String REF_XM_ORDER = RequestUtils.getStringParameter(request, "REF_XM_ORDER");
		String HTBH = RequestUtils.getStringParameter(request, "HTBH");
		String TZZT = RequestUtils.getStringParameter(request, "TZZT");
		String HKZT = RequestUtils.getStringParameter(request, "HKZT");
	
		parms.put("userId", userId);
		parms.put("TZSJ_START", TZSJ_START);
		parms.put("TZSJ_END", TZSJ_END);
		parms.put("HKZT", HKZT);
		parms.put("REF_XM_ORDER", REF_XM_ORDER);
		parms.put("TZZT", TZZT);
		parms.put("HTBH", HTBH);
		
	
		// 查询数据
		List list = p2pjhntzxiService.queryJhntzxi(parms);
		List<String[]> dataList = com.bringway.p2p.constant.ExcelHeadConstant.getJhntzxiExportSearchResultData(list);
		// 导出数据到Excel文件
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		ExcelSheet sheet = new ExcelSheet();
		sheet.setBodyList(dataList);
		sheet.setSheetName(DateUtils.getCurrentDataStr()+"计划内投资信息");
		sheetList.add(sheet);
		OutputStream os = response.getOutputStream();
		String fileName = DateUtils.getCurrentDataStr() + "计划内投资信息.xls";
		super.setDownLoadResponseHeader(request, response, fileName);
		response.setContentType("application/vnd.ms-excel");
		
		// 导出EXCEL
		ExcelOperUtil.writeExcel(sheetList, os);
		return null;
	}
}
